﻿@{
    ViewData["Title"] = "审批";
    var aGroupCode = ViewData["aGroupCode"] as string;
}
@model MainMenu[]

<div class="layui-layout layui-layout-admin">
    <div class="layui-logo layui-hide-xs layui-bg-black">移动审批</div>


    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">审批</a>
                    <dl class="layui-nav-child">
                        <dd class="layui-this"><a href="/home/index">单笔审批</a></dd>
                        <dd><a href="/home/groupindex">组合审批</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item"><a href="javascript:;">转授权</a></li>
            </ul>
        </div>
    </div>

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">

                <div class="layui-form toolbar">
                    <div class="layui-inline">
                        <input name="aGroupCode" id="aGroupCode" type="hidden" value="@aGroupCode">
                        <label class="layui-form-label w-auto">审批类型：</label>
                        <div class="layui-input-inline mr0">
                            <select name="aTargetType">
                                @foreach (var menu in Model)
                                {
                                    if (string.IsNullOrWhiteSpace(menu.Code))
                                    {
                                        <option value="@menu.Code">@menu.Display</option>
                                    }
                                    else
                                    {
                                        <option value="@menu.Code" selected>@menu.Display</option>
                                    }
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">日期：</label>
                        <div class="layui-input-inline mr0">
                            <input id="time" name="time" class="layui-input" type="text" readonly="readonly" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchTbBas" lay-submit>
                            <i class="layui-icon">&#xe615;</i>查询
                        </button>
                    </div>


                    <table class="layui-hide" id="demo" lay-filter="test"></table>

                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="trade">交易信息</a>
                        <a class="layui-btn layui-btn-xs" lay-event="flowchart">流程</a>
                        <a class="layui-btn layui-btn-xs" lay-event="tracelog">日志</a>
                        <a class="layui-btn layui-btn-xs" lay-event="limit">限额</a>
                        <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
                        <a class="layui-btn layui-btn-xs" lay-event="target">指标</a>
                    </script>

                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">审批：</label>
                        <div class="layui-input-inline mr0">
                            <select name="aDataTypeCurrent" id="aDataTypeCurrent">
                                <option value="">--请选择--</option>
                                <option value="0">等待审批</option>
                                <option value="1">待会签</option>
                                <option value="2">提前审批</option>
                                <option value="3">补审批</option>
                                <option value="4">已审核</option>
                                <option value="5">已会签</option>
                                <option value="6">等待签核</option>
                                <option value="7">已签核</option>
                                <option value="9">已签核</option>
                            </select>
                        </div>

                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline mr0">
                            <select name="aAction" id="aAction">
                                <option value="">--请选择--</option>
                                <option value="0">直接同意</option>
                                <option value="1">同意继续</option>
                                <option value="2">拒绝</option>
                                <option value="3">会签拒绝</option>
                                <option value="4">代签复核</option>
                                <option value="5">同意审批</option>
                            </select>
                        </div>
                        <a class="layui-btn icon-btn" id="agree">
                            审批前检查
                        </a>
                        <a class="layui-btn icon-btn" id="refuse">
                            审批操作
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div id="tplview" style="display: none;"></div>
    <script id="tpldemo" type="text/html">
        <ul>
        {{# if(d!=null&&d.length>0){}}
        {{# layui.jquery(d).each(function(index,item){ }}
        <div class="layui-inline layui-col-md5">
            <label class="layui-form-label">{{item.name}}:</label>
            <div class="layui-input-block">
                <label class="layui-form-label">{{item.txt}}</label>
            </div>
        </div>
        {{# }); }}
        {{# } }}
        </ul>
    </script>
    <script>
            //JS
            layui.use(['element','laydate', 'layer', 'util', 'form', 'table', 'laytpl'], function(){
              var element = layui.element
                  ,layer = layui.layer
                  ,util = layui.util
                  ,$ = layui.$
                  ,table = layui.table
                  ,laytpl = layui.laytpl
                  ,form = layui.form
                  ,laydate = layui.laydate;

            form.render('select');

            // 搜索按钮点击事件
            form.on('submit(formSubSearchTbBas)', function (data) {
                insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
            });

              //头部事件
              util.event('lay-header-event', {
                //左侧菜单事件
                menuLeft: function(othis){
                  layer.msg('展开左侧菜单的操作', {icon: 0});
                }
                ,menuRight: function(){
                  layer.open({
                    type: 1
                    ,content: '<div style="padding: 15px;">处理右侧面板的操作</div>'
                    ,area: ['260px', '100%']
                    ,offset: 'rt' //右上角
                    ,anim: 5
                    ,shadeClose: true
                  });
                }
              });

            //渲染时间控件
            laydate.render({
                elem: '#time',
                range: true
            });

              //执行一个 table 实例
            var insTb = table.render({
                elem: '#demo'
                ,height: 400
                ,url: '/api/approve/GetTaskNodeLstByPage?aGroupCode=@aGroupCode' //数据接口
                ,page: true //开启分页
                ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                //,totalRow: true //开启合计行
                ,cols: [[ //表头
                  {type: 'checkbox', fixed: 'left'}
                  ,{field: 'nid', title: '审批节点编号', width:120, fixed: 'left'}
                  ,{field: 'tid', title: '审批编号', width:80, sort: true}
                  ,{field: 'gR_ROLE', title: '审批角色编号', width: 90, sort: true}
                  ,{field: 'gR_USER', title: '审批用户', width: 90, sort: true}
                  ,{field: 'id', title: '交易编号', width:80, sort: true}
                  ,{field: 'isccrole', title: '是否会签', width: 80, sort: true, templet: function (d) {
                      if (d.isccrole) {
                          return "是";
                      } else {
                          return "否"
                      }
                  }}
                  ,{field: 'ty', title: '交易方向', width:150}
                  ,{field: 'or', title: '交易日期', width: 200}
                  ,{width: 550, align:'left', toolbar: '#barDemo'}
                ]]
              });

            table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

                if(layEvent === 'trade'){ //交易详情
                    var data = obj.data; //获得当前行数据

                    var getTpl = tpldemo.innerHTML
                    ,view = document.getElementById('tplview');
                    laytpl(getTpl).render(data.r, function(htmlstr){
                        tplview.innerHTML = htmlstr;
                        layer.open({
                            type: 1
                            ,title: '交易信息[' + data.nid + ']'
                            ,content: htmlstr
                            ,area: ['50%', '100%']
                            ,offset: 'rt' //右上角
                            ,anim: 5
                            ,shadeClose: true
                        });
                    });
                } else if(layEvent === 'flowchart'){ //流程
                    layer.open({
                        type: 2
                        ,title: '审批流程图[' + data.nid + ']'
                        ,area: ['100%', '100%']
                        ,content: '/home/approvetree?tid=' + data.tid
                    });
                } else if(layEvent === 'tracelog'){ //日志
                    layer.open({
                        type: 2
                        ,title: '审批日志[' + data.nid + ']'
                        ,area: ['100%', '100%']
                        ,content: '/home/tracelog?tid=' + data.tid
                    });
                } else if(layEvent === 'limit'){ //限额
                    layer.open({
                        type: 2
                        ,title: '限额[' + data.nid + ']'
                        ,area: ['100%', '100%']
                        ,content: '/home/limit?tid=' + data.tid
                    });
                } else if(layEvent === 'detail'){ //详情
                    layer.open({
                        type: 2
                        ,title: '详情[' + data.nid + ']'
                        ,area: ['100%', '100%']
                        ,content: '/home/detail?oid=' + data.id
                    });
                }else if(layEvent === 'target'){ //指标
                    layer.open({
                        type: 2
                        ,title: '指标[' + data.nid + ']'
                        ,area: ['100%', '100%']
                        ,content: '/home/target?tid=' + data.tid
                    });
                }
            });

            var actionIdx = ["直接同意", "同意继续", "拒绝", "会签拒绝", "代签复核", "同意审批" ]

            //审批前检查
            $("#agree").click(function () {
                var nids = [];
                var checkData = table.checkStatus('demo').data;
                if (checkData.length <= 0) {
                    layer.msg("请勾选要操作的数据", { icon: 2 });
                    return;
                }
                $(checkData).each(function (index, item) {
                    nids.push(item.nid);
                });
                var aDataTypeCurrent = $("#aDataTypeCurrent option:selected").val();
                layer.load(2);
                $.ajax({
                    url: '/api/Approve/MobileCheckBeforeActionByNodeId_Batch',
                    data: JSON.stringify({ nids: nids, aDataTypeCurrent: aDataTypeCurrent }),
                    type: 'post',
                    async: true,
                    dataType: 'json',
                    contentType: "application/json",
                    complete: function (obj) {
                        layer.closeAll('loading');
                    },
                    success: function (result) {
                        if (result.rt.re.rc=="0") {
                            layer.confirm('继续操作', {
                              btn: [result.rt.action.agreename, '关闭']
                              ,yes: function(index, layero){
                                var action = -1;
                                for(var i = 0; i<actionIdx.length; i++){
                                    if(actionIdx[i] == result.rt.action.agreename){
                                        action = i;
                                        break;
                                    }
                                }
                                if(action >= 0){

                                    layer.prompt({
                                        formType: 2,
                                        value: '',
                                        title: '审批备注',
                                        area: ['300px', '100px'] //自定义文本域宽高
                                    }, function (value, index, elem) {
                                            layer.load(2);
                                            $.ajax({
                                                url: '/api/Approve/MobileApproveTaskByNodeId_Batch',
                                                data: JSON.stringify({ nids: nids, aDataTypeCurrent: aDataTypeCurrent, aAction: action, actionNote: value }),
                                                type: 'post',
                                                async: true,
                                                dataType: 'json',
                                                contentType: "application/json",
                                                complete: function (obj) {
                                                    layer.closeAll('loading');
                                                },
                                                success: function (result) {
                                                    if (result.success) {
                                                        layer.msg(result.message, { icon: 1 });
                                                        insTb.reload({}, 'data');
                                                    } else {
                                                        layer.msg(result.message, { icon: 2 });
                                                    }
                                                    layer.close(index);
                                                }
                                            });
                                    });
                                }
                              }
                              ,btn2: function(index, layero){
                                layer.close(index);
                              }
                            });
                        } else {
                            layer.msg('不支持当前操作', { icon: 2 });
                        }
                    }
                });
            });

            //审批操作
            $("#refuse").click(function () {
                var nids = [];
                var aAction = $("#aAction option:selected").val();
                if (aAction == null || aAction == "") {
                    layer.msg("请选择审批动作", { icon: 2 });
                    return;
                }
                var checkData = table.checkStatus('demo').data;
                if (checkData.length <= 0) {
                    layer.msg("请勾选要操作的数据", { icon: 2 });
                    return;
                }
                $(checkData).each(function (index, item) {
                    nids.push(item.nid);
                });
                var aDataTypeCurrent = $("#aDataTypeCurrent option:selected").val();
                layer.prompt({
                    formType: 2,
                    value: '',
                    title: '请输入审批备注',
                    area: ['300px', '100px'] //自定义文本域宽高
                }, function (value, index, elem) {
                        layer.load(2);
                        $.ajax({
                            url: '/api/Approve/MobileApproveTaskByNodeId_Batch',
                            data: JSON.stringify({ nids: nids, aDataTypeCurrent: aDataTypeCurrent, aAction: parseInt(aAction), actionNote: value }),
                            type: 'post',
                            async: true,
                            dataType: 'json',
                            contentType: "application/json",
                            complete: function (obj) {
                                layer.closeAll('loading');
                            },
                            success: function (result) {
                                if (result.success) {
                                    layer.msg(result.message, { icon: 1 });
                                    insTb.reload({}, 'data');
                                } else {
                                    layer.msg(result.message, { icon: 2 });
                                }
                                layer.close(index);
                            }
                        });
                });
            });
        });
    </script>
